const axios = require('axios');
const FormData = require('form-data');
const fs = require('fs');

// 测试文件上传API
async function testFileUpload() {
  try {
    // 创建一个简单的测试文件
    const testFilePath = './test-upload.txt';
    fs.writeFileSync(testFilePath, 'This is a test file for upload');
    console.log('测试文件已创建');
    
    // 准备表单数据
    const formData = new FormData();
    formData.append('file', fs.createReadStream(testFilePath));
    
    // 设置请求头
    const headers = {
      ...formData.getHeaders(),
      'Content-Type': 'multipart/form-data'
    };
    
    console.log('开始上传文件...');
    // 发送请求
    const response = await axios.post(
      'http://localhost:8081/api/file/upload',
      formData,
      {
        headers,
        timeout: 30000
      }
    );
    
    console.log('上传成功! 响应:', response.data);
    
    // 清理测试文件
    fs.unlinkSync(testFilePath);
    console.log('测试文件已删除');
  } catch (error) {
    console.error('上传失败:', error.response ? error.response.data : error.message);
  }
}

// 执行测试
testFileUpload();